﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TomShane.Neoforce.Controls;

namespace ShootLife2D.GUI.Controls
{
    public class AnimatedImageBox : ImageBox
    {
        public Vector2 FrameSize;
        public int Frames;
        private Rectangle _currentFrame;
        private int _currentIntFrame = 0;
        public float UpdateTime = 0.03f;
        private float _timeLeft;
        public AnimatedImageBox(Vector2 frameSize, Texture2D image, Manager manager)
            : base(manager)
        {
            Image = image;
            FrameSize = frameSize;
            _currentFrame = new Rectangle(0, 0, (int)FrameSize.X, (int)FrameSize.Y);
        }
        protected override void InitSkin()
        {
            base.InitSkin();

        }
        protected override void DrawControl(Renderer renderer, Rectangle rect, GameTime gameTime)
        {
            Rectangle rc1 = Rectangle.Empty;
            if (Image != null)
            {
                rc1 = DrawingRect;
                renderer.Draw(Image, rc1, _currentFrame, Color.White);
            }
        }
        protected override void Update(GameTime gameTime)
        {
            _timeLeft += gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
            if (_timeLeft >= UpdateTime)
            {
                _timeLeft = 0;
                int yFrame = _currentIntFrame / (Image.Width / (int)FrameSize.X);
                int xFrame = _currentIntFrame % (Image.Width / (int)FrameSize.X);
                _currentFrame = new Rectangle((int)(xFrame * FrameSize.X), (int)(yFrame * FrameSize.Y), (int)FrameSize.X, (int)FrameSize.Y);
                _currentIntFrame++;
                _currentIntFrame %= Frames;
            }
            Invalidate();
            base.Update(gameTime);
        }
        public void UpdateImg(GameTime gameTime)
        {

        }
    }
}
